<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.ByteArrayInputStream" %>
 <%@ page import="org.json.simple.JSONObject" %>
 <%@ page import="org.json.simple.parser.JSONParser" %>
 <%@ page import="org.json.simple.JSONArray" %>
 <%@ page import="com.netcracker.studportal.server.facade.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Admin View - Add Student</title>
<link href="style/css/addStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
	if((session.getAttribute("username") == null) || ("").equals(session.getAttribute("username")))
	{
		response.sendRedirect("login.jsp");
	}

	request.setCharacterEncoding("UTF-8");
	
	FacadeAdmin fa = new FacadeAdmin();
	JSONParser parser = new JSONParser();
	String resultMessage = null;
%>
<div class="header">
    	<div class="wrapper">
    	<a href = "<% out.print("indexAdmin.jsp"); %>" >
        	<div class="logo"> 
        	<div class="exit"><a href = "<% out.print("logout.jsp"); %>" ><img src="style/img/exit.jpg"></a> </div> 
            <a href = "<% out.print("indexAdmin.jsp"); %>" ><img src="style/img/logo.png" class="log"></a>
            </a>
            </div>
</div>
    <div class="content">
    	<div class="wrapper">
        	<div class="left">
            	<ul class="top-menu">
                    <li><a href="<% out.print("admin.jsp?action=addStudent"); %>">Добавить нового студента</a></li>
                    <li><a href="<% out.print("admin.jsp?action=addProfessor"); %>">Добавить преподавателя</a></li>
                    <li><a href="<% out.print("admin.jsp?action=addExam"); %>">Добавить экзамен</a></li>
                    <li><a href="<% out.print("admin.jsp?action=AssignExam"); %>">Назначить экзамен</a></li>
       
            	</ul>
            </div>
            <div class="right">
            	<div class="input_data">
                	<form method="post" action="AddStudent.jsp?action=add">
			
			<input type="text" name="login"> Логин <br>
			<input type="password" name="password"> Пароль <br>
			<input type="text" name="first_name"> Имя <br>
			<input type="text" name="last_name"> Фамилия <br>
			<input type="text" name="birthday"> День Рождения <br>
			<input type="text" name="course"> Курс <br>
			<input type="text" name="group_num"> Номер Группы <br>
			<select name="faculty_id" style="width: 155px; ">
			<option value=""></option>
				<%
					String faculty=fa.getAllFaculty();
					Object ob = parser.parse(faculty);
					JSONArray array = (JSONArray)ob;
					Iterator iter = array.iterator();
					while(iter.hasNext()){
						JSONObject data = (JSONObject)iter.next();
						String id = data.get("faculty_id").toString();
						String fac_name = data.get("faculty_name").toString();
						out.println("<option value=\""+id+"\">"+fac_name+"</option>");
					}
				%>
			</select> Факультет <br>
			<input type="text" name="note"> Доп информация <br>
			
			<input type="submit" value="Добавить">
	</form>	
<%
if ("add".equals(request.getParameter("action"))) {
	String login = null;
	String password = null;
	String f_name = null;
	String l_name = null;
	String course = null;
	String birthday = null;
	String group_num = null;
	String faculty_id = null;
	String note = null;
	if (
		(request.getParameter("login") == null || request.getParameter("login").isEmpty()) ||
		(request.getParameter("password") == null || request.getParameter("password").isEmpty()) ||
		(request.getParameter("first_name") == null || request.getParameter("first_name").isEmpty()) ||
		(request.getParameter("last_name") == null || request.getParameter("last_name").isEmpty()) ||
		(request.getParameter("course") == null || request.getParameter("course").isEmpty()) ||
		(request.getParameter("birthday") == null || request.getParameter("birthday").isEmpty()) ||
		(request.getParameter("group_num") == null || request.getParameter("group_num").isEmpty()) ||
		(request.getParameter("faculty_id") == null || request.getParameter("faculty_id").isEmpty())
	   ) {
		out.println("<font color=red>Необходимо заполнить все поля!</font>");
	} else {
			login = request.getParameter("login");
			password = request.getParameter("password");
			f_name = request.getParameter("first_name");
			l_name = request.getParameter("last_name");
			course = request.getParameter("course");
			birthday = request.getParameter("birthday");
			group_num = request.getParameter("group_num");
			faculty_id = request.getParameter("faculty_id");
			note = request.getParameter("note");

			JSONObject obj = new JSONObject();
			obj.put("login", login);
			obj.put("password", password);
			obj.put("stud_first_name", f_name);
			obj.put("stud_last_name", l_name);
			obj.put("course", course);
			obj.put("birthday", birthday);
			obj.put("group_num", group_num);
			obj.put("faculty_id", faculty_id);
			obj.put("note", note);

			String param = obj.toString();
			String result = fa.addNewStudent(param);

			JSONObject jsonObj = (JSONObject) parser.parse(result);
			if ("success".equals(jsonObj.get("status"))) {
				resultMessage = "<font color=green>Студент успешно добавлен!</font>";
			} else {
				resultMessage = "<font color=red>При выполнении запроса возникли ошибки.</font>";
			}
	}
}
%>
<%
  if (resultMessage != null) {
	  out.print("Результат: " + resultMessage);
  }
%>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div class="footer">
    	<div class="wrapper">
        	<div class="copy">
            	<p>Студенческий портал. 2014 &copy; by NetCracker</p>
            </div>
        </div>
    </div>	
</body>
</html>